package com.arvin.service.serviceImpl;

import com.arvin.mapper.*;
import com.arvin.pojo.*;
import com.arvin.service.ExamService;
import com.arvin.service.ExemptService;
import com.arvin.service.StudentService;
import com.arvin.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ExamMapper examMapper;
    @Autowired
    private InterviewMapper interviewMapper;
    @Autowired
    private ExemptMapper exemptMapper;
    @Autowired
    private TimeMapper timeMapper;
    @Autowired
    private ClassMapper classMapper;

    /**
     * 保存报名信息
     * @param student
     */
    @Transactional
    public void saveStudent(Student student) {
        //判断竞赛信息是否为空
        if (student.getContest() != null) {
            studentMapper.deleteContest(student.getOldStudentId());
            for (Contest contest: student.getContest()){
                contest.setStudentId(student.getStudentId());
                log.info("竞赛信息{}",contest);
                studentMapper.saveContest(contest);
            }
        }
        studentMapper.saveStudent(student);
        classMapper.saveClass(student);
    }

    @Override
    public Student getStudentById(String studentId) {
        return studentMapper.findByStudentId(studentId);
    }

    /**
     * 通过学号查找学生信息
     * @param studentId
     * @return
     */
    @Transactional
    //通过学号查找
    public Student findByStudentId(String studentId) {
        Student student = studentMapper.findByStudentId(studentId);
        List<Contest> contestList = studentMapper.findByStudentIdToContest(studentId);
        student.setContest(contestList);
        log.info("竞赛信息{}",contestList);
        log.info("学生信息{}",student);
        return student;
    }

    /**
     * 保存笔试信息
     * @param exam
     */
    @Transactional
    public Exam saveExamInfo(Exam exam)  throws  NullPointerException{
        Map<String,Object> map = ThreadLocalUtil.get();
        String studentId = (String) map.get("StudentId");
        exam.setStudentId(studentId);

        Student student = studentMapper.findByStudentId(studentId);//获取考试id
        if (student.getExamId() == null){//判端id是否为空，为空就插入，不为空就更新数据
            exam.setExamState("未考试");
            examMapper.insertExamInfo(exam);
        }
        else {
            examMapper.updateExamInfo(exam);
        }
        Time time = timeMapper.getTime();
        student.setClassName(time.getClassName());
        student.setExamId(exam.getExamId());
        student.setOldStudentId(studentId);
        log.info("保存学生信息{}",student);
        studentMapper.saveStudent(student);
        return exam;
    }

    /**
     * 保存免试人员信息
     * @param exempt
     */
    @Transactional
    public Exempt saveExempt(Exempt exempt) {
        Map<String,Object> map = ThreadLocalUtil.get();
        String studentId = (String) map.get("StudentId");
        exempt.setStudentId(studentId);

        Student student = studentMapper.findByStudentId(studentId);
        if (student.getExemId() == null){
            exempt.setExemState("审核中");
            exemptMapper.saveExempt(exempt);
        }else {
            exemptMapper.updateExemInfo(exempt);
        }
        Time time = timeMapper.getTime();
        student.setClassName(time.getClassName());
        student.setExemId(exempt.getExemId());
        student.setOldStudentId(studentId);
        studentMapper.saveStudent(student);
        return exempt;
    }

    /**
     * 获取免试信息
     * @param exemId
     * @return
     */
    public Exempt getExemptInfo(Integer exemId) {
        Exempt exempt = exemptMapper.findByExempId(exemId);
        return exempt;
    }

    /**
     * 通过examId查找笔试信息
     * @param examId
     * @return
     */

    public Exam findByExamId(Integer examId) {
        Exam exam = examMapper.findByExamId(examId);
        return exam;
    }

    /**
     * 通过timeId查找管理员设置时间信息
     * @param timeId
     * @return
     */
    @Override
    public Time findByTimeId(Integer timeId) {
        Time time = timeMapper.getTime();
        return time;
    }

    /**
     *保存面试信息
     * @param interview
     */
    @Transactional
    public Interview saveInterviewInfo(Interview interview) {
        Map<String,Object> map = ThreadLocalUtil.get();
        String studentId = (String) map.get("StudentId");
        interview.setStudentId(studentId);

        Student student = studentMapper.findByStudentId(studentId);
        if (student.getIntvId() == null){
            interview.setIntvState("未面试");
            interviewMapper.saveInterviewInfo(interview);
        } else {
            interviewMapper.updateIntvInfo(interview);
        }
        Time time = timeMapper.getTime();
        student.setClassName(time.getClassName());
        student.setIntvId(interview.getIntvId());
        student.setOldStudentId(studentId);
        studentMapper.saveStudent(student);
        return interview;
    }

    /**
     * 查找面试信息
     * @param intvId
     * @return
     */
    public Interview findByIntvId(Integer intvId) {
        Interview interview = interviewMapper.findByIntvId(intvId);
        return interview;
    }

    /**
     * 通过考试日期获取预约面试时间信息
     * @param intvDate
     * @param studentId
     * @return
     */
    @Override
    public List<Interview> findByIntvDate(LocalDate intvDate,String studentId) {
        return interviewMapper.findByIntvDate(intvDate,studentId);
    }

    /**
     * 通过免试日期获取预约免试时间信息
     * @param exemDate
     * @param studentId
     * @return
     */
    @Override
    public List<Exempt> findByExemDate(LocalDate exemDate, String studentId) {
        return interviewMapper.findByExemDate(exemDate,studentId);
    }

    /**
     * 通过考试id清除考试信息
     * @param examId
     */
    @Override
    public void clearByExamId(Integer examId) {
        examMapper.clearByExamId(examId);
    }

    /**
     * 通过免试id清除免试信息
     * @param exemId
     */
    @Override
    public void clearByExemId(Integer exemId) {
        examMapper.clearByExemId(exemId);
    }

    /**
     * 通过面试id清除面试信息
     * @param intvId
     */
    @Override
    public void clearByIntvId(Integer intvId) {
        interviewMapper.clearByIntvId(intvId);
    }
}
